# Makefile for compiling Go project in WSL (Windows Subsystem for Linux)

# 设置Go环境变量
GOOS=linux
GOARCH=amd64
CGO_ENABLED=1

# 项目名称和输出文件
PROJECT_NAME=myapp
OUTPUT=$(PROJECT_NAME)

# 主包路径（根据实际情况调整）
MAIN_PACKAGE=.

# 默认目标
all: tidy build

# 动态链接构建目标
build:
	@echo "Building $(PROJECT_NAME) for Linux (amd64) with dynamic linking..."
	GOOS=$(GOOS) GOARCH=$(GOARCH) CGO_ENABLED=$(CGO_ENABLED) \
	go build -o $(OUTPUT) $(MAIN_PACKAGE)
	@echo "Build complete. Output: $(OUTPUT)"

# 静态链接构建目标
build-static:
	@echo "Building $(PROJECT_NAME) for Linux (amd64) with static linking..."
	CGO_ENABLED=1 GOOS=$(GOOS) GOARCH=$(GOARCH) \
	go build -a -ldflags '-extldflags "-static"' -o $(OUTPUT)_static $(MAIN_PACKAGE)
	@echo "Static build complete. Output: $(OUTPUT)_static"

# 清理目标
clean:
	@echo "Cleaning up..."
	@rm -f $(OUTPUT) $(OUTPUT)_static
	@echo "Clean complete."

# 运行测试
test:
	@echo "Running tests..."
	GOOS=$(GOOS) GOARCH=$(GOARCH) CGO_ENABLED=$(CGO_ENABLED) \
	go test ./...

# 初始化Go模块
init:
	@echo "Initializing Go module..."
	@go mod init demo
	@echo "Go module initialized."

# 运行 go mod tidy
tidy:
	@echo "Running go mod tidy..."
	@go mod tidy
	@echo "Dependencies tidied."

# 安装依赖（如果需要的话）
deps:
	@echo "Installing build dependencies..."
	@sudo apt-get update
	@sudo apt-get install -y build-essential
	@echo "Dependencies installed."

# 显示项目结构
tree:
	@echo "Project structure:"
	@tree -L 2 -I "vendor|.git"

# 帮助信息
help:
	@echo "Available targets:"
	@echo "  build        - Build the project with dynamic linking"
	@echo "  build-static - Build the project with static linking"
	@echo "  clean        - Remove built files"
	@echo "  test         - Run tests"
	@echo "  init         - Initialize Go module"
	@echo "  tidy         - Run go mod tidy to manage dependencies"
	@echo "  deps         - Install build dependencies (requires sudo)"
	@echo "  tree         - Display project structure"
	@echo "  all          - Run tidy and then build (default)"
	@echo "  help         - Show this help message"

.PHONY: all build build-static clean test init tidy deps tree help
